﻿// 功能：实现MzFC中StatusBar控件
// 描述：
// 作者：温伟鹏
// 日期：2010/5/25 13:26:57

using System;
using System.Runtime.InteropServices;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 跟踪条控件（实现MzFC中StatusBar控件）
    /// </summary>
    public class TrackBar : Control
    {
        #region DllImport
        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiStatusBarCtor();

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiStatusBarEnableValueLock(IntPtr pObj, bool bLock);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiStatusBarIsValueLockEnable(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiStatusBarGetPadPos(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiStatusBarSetPadPos(IntPtr pObj, int iPadPos);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiStatusBarGetPosMax(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiStatusBarGetPosMin(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiStatusBarSetRange(IntPtr pObj, int min, int max);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiStatusBarGetLockMax(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiStatusBarGetLockMin(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiStatusBarSetValueLock(IntPtr pObj, int min, int max);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiStatusBarSetStyle(IntPtr pObj, int barStyle);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiStatusBarSetTextWidth(IntPtr pObj, int textWidth);

        #endregion

        #region 事件
        /// <summary>
        /// 当跟踪条控件的当前值被更改后触发此事件
        /// </summary>
        public event EventHandler ValueChanged;
        #endregion

        #region 常量
        /// <summary>
        /// 跟踪条的高度
        /// </summary>
        public const int HEIGHT = 50;
        #endregion

        #region 私有变量

        private TrackBarStyle barStyle;

        private int min;
        private int max;
        private int currentValue;
        private int lockMin = -1;
        private int lockMax = -1;
        private int textWidth;

        #endregion

        #region 公共属性
        /// <summary>
        /// 获取或设置一个值，指示跟踪条是否启用值锁定
        /// </summary>
        public bool EnableValueLock
        {
            get
            {
                return UiStatusBarIsValueLockEnable(Handle);
            }
            set
            {
                UiStatusBarEnableValueLock(Handle, value);
            }
        }
        /// <summary>
        /// 获取或设置跟踪条控件样式
        /// </summary>
        public TrackBarStyle TrackBarStyle
        {
            get
            {
                return barStyle;
            }
            set
            {
                if (value != this.barStyle)
                {
                    this.barStyle = value;
                    UiStatusBarSetStyle(Handle, (int)value);
                }
            }
        }
        /// <summary>
        /// 获取或设置跟踪条的最大值（默认值：100）
        /// </summary>
        public int Max
        {
            get
            {
                return this.max;
            }
            set
            {
                if (value != this.max)
                {
                    if (EnableValueLock)
                    {
                        if (value < LockMin || value < Min)
                        {
                            value = Math.Min(LockMin, Min) + 1;
                        }
                        if (LockMax > value)
                        {
                            LockMax = value;
                        }
                    }
                    else
                    {
                        if (value < Min)
                        {
                            value = Min + 1;
                        }
                    }

                    this.max = value;
                    UiStatusBarSetRange(Handle, Min, value);
                }
            }
        }
        /// <summary>
        /// 获取或设置跟踪条最小值（默认值：0）
        /// </summary>
        public int Min
        {
            get
            {
                return min;
            }
            set
            {
                if (value != this.min)
                {
                    if (EnableValueLock)
                    {
                        if (value > LockMax || value < Max)
                        {
                            value = Math.Min(LockMax, Max) - 1;
                        }
                        if (LockMin < Min)
                        {
                            LockMin = value;
                        }
                    }
                    else
                    {
                        if (value < Max)
                        {
                            value = Max - 1;
                        }
                    }
                    this.min = value;
                    UiStatusBarSetRange(Handle, value, Max);
                }
            }
        }
        /// <summary>
        /// 获取或设置跟踪条当前值（默认值：0）
        /// </summary>
        public int Value
        {
            get
            {
                this.currentValue = UiStatusBarGetPadPos(Handle);

                return this.currentValue;
            }
            set
            {
                if (value != this.currentValue)
                {
                    if (EnableValueLock)
                    {
                        if (value < LockMin || value < Min)
                        {
                            value = Math.Min(LockMin, Min);
                        }
                        if (value > LockMax || value > Max)
                        {
                            value = Math.Min(LockMax, Max);
                        }
                    }
                    else
                    {
                        if (value < Min)
                        {
                            value = Min;
                        }
                        else if (value > Max)
                        {
                            value = Max;
                        }
                    }

                    this.currentValue = value;

                    UiStatusBarSetPadPos(Handle, value);
                }
            }
        }
        /// <summary>
        /// 获取或设置跟踪条最小的锁定值（当EnableValueLock=true时，才有效）
        /// </summary>
        public int LockMin
        {
            get
            {
                return this.lockMin;
            }
            set
            {
                if (EnableValueLock)
                {
                    if (value != this.lockMin)
                    {
                        this.lockMin = value;
                    }
                }
            }
        }
        /// <summary>
        /// 获取或设置跟踪条最大的锁定值（当EnableValueLock=true时，才有效）
        /// </summary>
        public int LockMax
        {
            get
            {
                return this.lockMax;
            }
            set
            {
                if (EnableValueLock)
                {
                    if (value != this.lockMax)
                    {
                        this.lockMax = value;
                    }
                }
            }
        }
        /// <summary>
        /// 获取或设置跟踪条显示文本的宽度
        /// </summary>
        public int TextWidth
        {
            get
            {
                return this.textWidth;
            }
            set
            {
                if (value != this.textWidth)
                {
                    this.textWidth = value;
                    UiStatusBarSetTextWidth(Handle, value);
                }
            }
        }
        #endregion

        #region 构造函数

        public TrackBar()
        {
            Handle = UiStatusBarCtor();
            SetID();
            Height = 50;
            EnableValueLock = false;
            //Min = 0;
            //Max = 100;
            //Value = 0;
        }
        #endregion

        #region 函数
        /// <summary>
        /// 处理控件消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        protected override void WndProc(uint message, uint wParam, uint lParam)
        {
            if (message == MzUser.MZ_MOUSE_DOWN_MOVE)
            {
                OnValueChanged(EventArgs.Empty);
            }
            base.WndProc(message, wParam, lParam);
        }
        /// <summary>
        /// 引发 ValueChanged 事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnValueChanged(EventArgs e)
        {
            if (ValueChanged != null)
            {
                ValueChanged(this, EventArgs.Empty);
            }
        }
        #endregion
    }

    /// <summary>
    /// 跟踪条样式
    /// </summary>
    public enum TrackBarStyle : int
    {
        StatusBar = 0,
        ProgressBar = 1
    }
}
